package com.syxbbs.forum.exception;


import com.syxbbs.forum.common.AppResult;
import com.syxbbs.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//全局异常处理
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    //表示处理哪个异常
    @ExceptionHandler(ApplicationException.class)
    public AppResult ApplicationException(ApplicationException e){
        log.error(e.getMessage());
        //如果不为空
        if(e.getErrorResult()!=null){
            return e.getErrorResult();

        }
        if (e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failure(ResultCode.ERROR_SERVICES);

        }

        //全局异常处理
        return AppResult.failure(e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult exceptionHandler(Exception e){
        log.error(e.getMessage());
        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failure(ResultCode.ERROR_SERVICES);

        }
        return AppResult.failure(e.getMessage());
    }




}
